#include "commonpage.h"
#include "ui_commonpage.h"
#include"listitembox.h"

CommonPage::CommonPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommonPage)
{
    ui->setupUi(this);
    //设置水平滑条不显示
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    connect(ui->pageMusicList,&QListWidget::doubleClicked,this
            ,[=](const QModelIndex &index){ emit playMusicByIndex(this,index.row());});
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setCommonPage(const QString &text, const QString &imagePath)
{
    //给公共页设置标题
    ui->pageTitle->setText(text);
    //给播放区设置图片
    ui->musicImageLabel->setPixmap(QPixmap(imagePath));
    //让图片的大小符合musiclabel的大小
    ui->musicImageLabel->setScaledContents(true);



}

void CommonPage::addMusicToMusicPage(MusicList &musicList)
{
    musicListOfPage.clear();
    for(auto music:musicList)
    {
        switch(pagetype)
        {
         case LIKE_PAGE:
            if(music.getIsLike())
                musicListOfPage.push_back(music.getMusicId());
             break;
        case LOCAL_PAGE:
             musicListOfPage.push_back(music.getMusicId());
            break;
         case HISTORY_PAGE:
            if(music.getIsHistory())
             musicListOfPage.push_back(music.getMusicId());
            break;
        default:
            break;
        }

    }
}


void CommonPage::reFresh(MusicList &musicList)
{
    //每个页面管理自己的歌曲 用qvector管理uuid就行
    ui->pageMusicList->clear();
    //根据不同页面的特点,将音乐添加到对应的页面中
    addMusicToMusicPage(musicList);

    for(auto musicId:musicListOfPage)
    {
        //根据musicId获取music对象
        auto it=musicList.findMusicByMusicId(musicId);
        if(it==musicList.end())
            continue;

        //it为对象的迭代器
        //设置音乐到对应的page中

          ListItemBox* item=new ListItemBox(this);
          item->setAlumnName(it->getMusicAlumn());
          item->setMusicName(it->getMusicName());
          item->setMusicSigner(it->getMusicSinger());
          item->setLikeICon(it->getIsLike());
          QListWidgetItem* listItem =new QListWidgetItem(ui->pageMusicList);
          listItem->setSizeHint(QSize(ui->pageMusicList->width(),45));
          ui->pageMusicList->setItemWidget(listItem,item);
          connect(item,&ListItemBox::setIsLike,this,[=](bool IsLike){
              emit updateLikeMusic(IsLike,it->getMusicId());
          });

    }
    //刷新一下重绘事件
    ui->pageMusicList->repaint();
}

void CommonPage::setPageType(PageType pageType)
{
    this->pagetype= pageType;
}

PageType CommonPage::getPageType()
{
    return pagetype;
}

//根据音乐列表中音乐所属的页面,将音乐添加到playList中
void CommonPage::addMusicToPlayList(MusicList &musicList, QMediaPlaylist *playList)
{
    for(auto e:musicList)
    {
        switch (pagetype)
        {
        case LOCAL_PAGE:
            playList->addMedia(e.getMusicUrl());
                break;
        case LIKE_PAGE:
            if(e.getIsLike())
                playList->addMedia(e.getMusicUrl());
            break;
        case HISTORY_PAGE:
            if(e.getIsHistory())
                playList->addMedia(e.getMusicUrl());
            break;
        default:
            break;
        }
    }

}

QString CommonPage::getMusicIdByIndex(int index)
{
    if(index<0||index>=musicListOfPage.size())
    {
        qDebug()<<"index error";
        return QString();
    }
    return musicListOfPage[index];

}

void CommonPage::setImageLabel(QPixmap map)
{
    ui->musicImageLabel->setPixmap(map);
    ui->musicImageLabel->setScaledContents(true);
}


//点击播放全部按钮时,根据page的不同判断在那个页面播放对应的歌曲
void CommonPage::on_playAllBtn_clicked()
{
    emit playAll(pagetype);

}
